
// 3. 获取当前时间--结束
export const getDateEnd = () => {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  month = month < 10 ? '0' + month : month // 月小于10，加0
  day = day < 10 ? '0' + day : day // day小于10，加0
  return `${year}-${month}-${day}`
}
// 3.1 获取当前时间--开始
export const getDateStart = () => {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  //   let day = date.getDate()
  month = month < 10 ? '0' + month : month // 月小于10，加0
  //   day = day < 10 ? '0' + day : day // day小于10，加0
  return `${year}-${month}-01`
}
// 3.2 获取当前时间--开始
export const getDateStartTime = () => {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  //   let day = date.getDate()
  month = month < 10 ? '0' + month : month // 月小于10，加0
  //   day = day < 10 ? '0' + day : day // day小于10，加0
  return `${year}-${month}-01 00:00:00`
}
// 3.3获取当前时间--结束时间
export const getDateEndTime = () => {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  month = month < 10 ? '0' + month : month // 月小于10，加0
  day = day < 10 ? '0' + day : day // day小于10，加0
  return `${year}-${month}-${day} 23:59:59`
}
// 3.4  // 获取当前一周的时间

export const getWeekDay = (dateString) => {
  const dateStringReg = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/

  if (dateString.match(dateStringReg)) {
    const presentDate = new Date(dateString)
    const today = presentDate.getDay() !== 0 ? presentDate.getDay() : 7

    return Array.from(new Array(7), function(val, index) {
      return formatDate(new Date(presentDate.getTime() - (today - index - 1) * 24 * 60 * 60 * 1000))
    })
  } else {
    throw new Error('dateString should be like "yyyy-mm-dd" or "yyyy/mm/dd"')
  }

  function formatDate(date) {
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
  }
}
// 3.5.一年的开始日期eg：2022-01-01 00：00:00
export const getYear = () => {
  const date = new Date()
  const year = date.getFullYear()
  return `${year}-01-01 00:00:00`
}

// 3.6.获取今天日期的前一天日期
// 思路：获取今天是星期几/往前
// 获得星期一
export function getFirstDayOfWeek(date) {
  var weekday = date.getDay() || 7 // 获取星期几,getDay()返回值是 0（周日） 到 6（周六） 之间的一个整数。0||7为7，即weekday的值为1-7
  date.setDate(date.getDate() - weekday + 1)// 往前算（weekday-1）天，年份、月份会自动变化
  // return date
  return date
}

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}
